package com.liner.homework;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Homework02 {
    static Scanner scanner = new Scanner(System.in);
    static Random rand = new Random();

    public static void main(String[] args) {
        /*
        1. 猜数字
        2. 生成N位验证码
        3. 找到2到100之内的所有素数
        4. 飞机票打折
        5. 评委打分:去掉最高分和最低分之后的平均分
*/

        //猜数字
        System.out.println("系统生成0~100的数字，请猜测");
        int random = rand.nextInt(101);
        guessing(random);


        //生成N位验证码
        System.out.print("请输入要生成几位数验证码:");
        int len = scanner.nextInt();
        System.out.println("已生成" + generateVeriCode(len));

        //找到2到100之内的所有素数
        for (int i = 2; i <= 100; i++) {
            prime(i);
        }

        System.out.println();

        //飞机票打折
        System.out.print("请输入飞机票原价:");
        int price = scanner.nextInt();
        System.out.print("请输入当前月份:");
        int month = scanner.nextInt();
        System.out.print("请输入所选机舱(1.经济舱2.商务舱3.头等舱)");
        String ship = scanner.next();
        System.out.println("最终票价：" + getFinalPrice(price, month, ship));


        //评委打分:去掉最高分和最低分之后的平均分
        System.out.print("请输入本场评委数:");
        int number = scanner.nextInt();
        double[] scores = inputScore(number);
        double avg = getAvg(scores);
        System.out.println("最终评委打分的平均分是:" + avg);

    }

    /**
     * 算出平均分
     *
     * @param scores
     * @return
     */
    private static double getAvg(double[] scores) {

        double sum = getSum(scores);

        return sum / (scores.length - 2);
    }

    /**
     * 算出总分
     *
     * @param scores
     * @return
     */
    private static double getSum(double[] scores) {
        double sum = 0;
        Arrays.sort(scores);
        for (int i = 1; i < scores.length - 1; i++) {
            sum += scores[i];
        }
        return sum;
    }

    /**
     * 控制台输入分数
     *
     * @param number
     */
    private static double[] inputScore(int number) {
        double[] scores = new double[number];
        for (int i = 0; i < scores.length; i++) {
            System.out.print("请输入" + (i + 1) + "号评委分数:");
            scores[i] = scanner.nextDouble();
        }
        return scores;
    }

    /**
     * 购买打折机票
     *
     * @param price
     * @param month
     * @param ship
     * @return
     */
    private static double getFinalPrice(int price, int month, String ship) {
        double finalPrice = 0;

        if (price < 0) {
            System.out.println("请输入正确的票价");
        }
        if (month < 0 || month > 12) {
            System.out.println("请输入正确的月份");
        }

        if (month >= 5 && month <= 10) {
            //旺季
            switch (ship) {
                case "经济舱":
                    finalPrice = price * 0.8;
                    break;
                case "商务舱":
                    finalPrice = price * 0.85;
                    break;
                case "头等舱":
                    finalPrice = price * 0.9;
                    break;
                default:
                    System.out.println("请输入正确舱位");
            }

        } else {
            //淡季
            switch (ship) {
                case "经济舱":
                    finalPrice = price * 0.6;
                    break;
                case "商务舱":
                    finalPrice = price * 0.65;
                    break;
                case "头等舱":
                    finalPrice = price * 0.7;
                    break;
                default:
                    System.out.println("请输入正确舱位");
            }
        }
        return finalPrice;
    }

    /**
     * 素数
     *
     * @param num
     */
    private static void prime(int num) {
        boolean flag = true;
        for (int j = 2; j <= num / 2; j++) {
            if (num % j == 0) {
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.print(num + "\t");
        }
    }

    /**
     * 随机生成验证码
     *
     * @param len
     * @return
     */
    private static String generateVeriCode(int len) {
        String code = "";
        //数组初始化
        char[] chars = {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        };
        for (int i = 0; i < len; i++) {
            code += chars[rand.nextInt(chars.length)];
        }
        return code;
    }


    /**
     * 猜数字
     *
     * @param random
     */
    private static void guessing(int random) {
        while (true) {
            System.out.print("你的猜测是:");
            int guess = scanner.nextInt();
            if (random == guess) {
                System.out.println("恭喜你猜对了");
                break;
            } else if (random > guess) {
                System.out.println("很遗憾，猜小了");
            } else {
                System.out.println("很遗憾，猜大了");
            }

        }
    }
}
